//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>

#include "CombinedPatternsCathFASTAProfile.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  String*           filenameCathFASTAProfile         = NULL;
  String*           filenameLigandCathFASTAProfile   = NULL;
  String*           filenameExcludedCathFASTAProfile = NULL;
  String*           filenameOut                      = NULL;
  String*           nameRelation                     = NULL;
  
  int               positionInPatternFile;
  float             percentagePattern;
  float             percentageFalse;
  
  TListE <String>*  listFilenamePatternsCompact      = new TListE <String> ();
  TListE <String>*  listTypeOfData                   = new TListE <String> ();

  if (argc < 10) {
    
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./CreateARFFFileWithCompactPatternCombinedFile <Filename CATH FASTA Profile Complete>";
    cout <<                                               " <Filename CATH FASTA Profile Of Sequences in which the Patterns were extracted>";
    cout <<                                               " <Position of Patterns in a Pattern File (1->20%, 2->40%, 3->60%, 4->80%, 5->90%)>";
    cout <<                                               " <Percentage of patterns to use in the ARFF File>";
    cout <<                                               " <Percentage of false records in the ARFF File>";
    cout <<                                               " <Filename Out - ARFF File (Weka)>";
    cout <<                                               " <Filename CATH FASTA Profile with sequences to exclude (NULL)>";
    cout <<                                               " <Name of Relation in Weka File>";
    cout <<                                               " <Filename In with compact Patterns>";
    cout <<                                               " <Type Of File (Seq, CH, PH, SS, MT)>";
    cout <<                                               " [<Filename In with compact Patterns>";
    cout <<                                               " <Type Of File (Seq, CH, PH, SS, MT)> ...]";
    cout << endl;
    
    return 0;
    
  }
  else {
    
    filenameCathFASTAProfile         = new String(argv[1]);
    filenameLigandCathFASTAProfile   = new String(argv[2]);
    
    positionInPatternFile            = atoi(argv[3]);
    percentagePattern                = atof(argv[4]);
    percentageFalse                  = atof(argv[5]);

    filenameOut                      = new String(argv[6]);
    filenameExcludedCathFASTAProfile = new String(argv[7]);
    nameRelation                     = new String(argv[8]);
    
    for (int i = 9; i < argc; i = i + 2) {
      listFilenamePatternsCompact->Add(new String(argv[i]));
      listTypeOfData->Add(new String(argv[i+1]));
    }
    
    cout << "----------------------------------------------------------------------------------------------------------------" << endl;
    cout << "                                           Filename CATH FASTA Profile Complete : " << argv[1] << endl;
    cout << "    Filename CATH FASTA Profile Of Sequences in which the Patterns is extracted : " << argv[2] << endl;
    cout << "  Position of Patterns in Pattern File (1->20%, 2->40%, 3->60%, 4->80%, 5->90%) : " << argv[3] << endl;
    cout << "                                 Percentage of patterns to use in the ARFF File : " << argv[4] << endl;
    cout << "                                   Percentage of false records in the ARFF File : " << argv[5] << endl;
    cout << "                                                Filename Out - ARFF File (Weka) : " << argv[6] << endl;
    cout << "                          Filename CATH FASTA Profile with sequences to exclude : " << argv[7] << endl;
    cout << "                                                  Name of Relation in Weka File : " << argv[8] << endl;
    listFilenamePatternsCompact->SetInitial();
    listTypeOfData->SetInitial();
    while (listFilenamePatternsCompact->SetNext() && listTypeOfData->SetNext()) {
      cout << "                                              Filename In with compact Patterns : " << listFilenamePatternsCompact->GetCurrent()->Out() << endl;
      cout << "                                             Type Of File (Seq, CH, PH, SS, MT) : " << listTypeOfData->GetCurrent()->Out() << endl;
    }
    cout << "----------------------------------------------------------------------------------------------------------------" << endl;
    
  }
  
  CombinedPatternsCathFASTAProfile* combinedPatternsCathFASTAProfile = new CombinedPatternsCathFASTAProfile ();
  
  cout << "Preparing the files..." << endl;
  combinedPatternsCathFASTAProfile->FilenameCathFASTAProfile(filenameCathFASTAProfile);
  combinedPatternsCathFASTAProfile->FilenameLigandCathFASTAProfile(filenameLigandCathFASTAProfile);
  combinedPatternsCathFASTAProfile->FilenameOut(filenameOut);
  
  if (strcmp(filenameExcludedCathFASTAProfile->Out(), "NULL") != 0)
    combinedPatternsCathFASTAProfile->FilenameExcludeCathFASTAProfile(filenameExcludedCathFASTAProfile);
  
  listFilenamePatternsCompact->SetInitial();
  listTypeOfData->SetInitial();
  while (listFilenamePatternsCompact->SetNext() && listTypeOfData->SetNext()) {
    combinedPatternsCathFASTAProfile->ListFilenamePatternsCompact()->Add(new String(listFilenamePatternsCompact->GetCurrent()));
    combinedPatternsCathFASTAProfile->ListTypeOfData()->Add(new String(listTypeOfData->GetCurrent()));
  }
  
  cout << "Loading the data..." << endl;
  if (!combinedPatternsCathFASTAProfile->LoadData())
    cout << "Error loading data!" << endl;
  
  cout << "Writing the result in the output file..." << endl;
  if (!combinedPatternsCathFASTAProfile->CreateCombinedARFF(nameRelation, nameRelation, positionInPatternFile, percentagePattern, percentageFalse))
    cout << "Error creating the ARFF File!" << endl;
  
  cout << "Finished..." << endl << "Good Bye" << endl;
  
  if (combinedPatternsCathFASTAProfile)   delete combinedPatternsCathFASTAProfile;
  
  if (listFilenamePatternsCompact)        delete listFilenamePatternsCompact;
  if (listTypeOfData)                     delete listTypeOfData;
  
  if (filenameCathFASTAProfile)           delete filenameCathFASTAProfile;
  if (filenameLigandCathFASTAProfile)     delete filenameLigandCathFASTAProfile;
  if (filenameExcludedCathFASTAProfile)   delete filenameExcludedCathFASTAProfile;
  if (filenameOut)                        delete filenameOut;

  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

